/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.base.service;

import com.google.gson.JsonObject;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.base.entity.BusinessQueue;
import com.jeeplus.modules.base.entity.CountQueue;
import com.jeeplus.modules.base.mapper.GxsqBaseMapper;
import com.jeeplus.modules.work.utils.Send;
import com.jeeplus.modules.work.utils.WebSocketServer;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 排队操作记录Controller
 * @author azj
 * @version 2024-09-29
 */
@Service
@Transactional(readOnly = true)
public class GxsqBaseService {
	@Autowired
	private GxsqBaseMapper gxsqBaseMapper;
	@Autowired
	private Send send;
	@Autowired
	private WebSocketServer webSocketServer;
	/**
	 * getCardQueue
	 * 排队取号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@Transactional(readOnly = false)
	public AjaxJson getCardValue(String idcard, String name, String tell, String area, String typeid){
		HashMap<String, String> map = new HashMap <String, String>();
		// 判断业务是否在工作时间取票
		if(gxsqBaseMapper.checkTimes(typeid,area) >= 1){
			//判断是否未办结记录
			if(gxsqBaseMapper.checkQueueByCard(typeid,idcard) <= 0){
				//上锁 进行排队取号
				synchronized (this){
					//获取当前排队记录编号
					BusinessQueue queueStart = gxsqBaseMapper.findQueueStart(typeid, area);
					if(null == queueStart){
						return AjaxJson.error("暂无该业务配置！");
					}else{
						//获取当前编号
						if(StringUtils.isBlank(queueStart.getNum())){
							queueStart.setNum("1");
						}else{
							queueStart.setNum(String.valueOf(Integer.parseInt(queueStart.getNum())+1));
						}
						//排队编号填充
						if(queueStart.getNum().length() == 1){
							queueStart.setNumber(queueStart.getBusinesschar()+"00"+queueStart.getNum());
						}else if(queueStart.getNum().length() == 2){
							queueStart.setNumber(queueStart.getBusinesschar()+"0"+queueStart.getNum());
						}else{
							queueStart.setNumber(queueStart.getBusinesschar()+queueStart.getNum());
						}
						//其他数据填充
						queueStart.setId(UUID.randomUUID().toString());
						queueStart.setIdcard(idcard);
						queueStart.setName(name);
						queueStart.setTell(tell);
						queueStart.setTypeid(typeid);
						queueStart.setAreaid(area);
						queueStart.setCometime(DateUtils.getDateTime());
						//排队记录 叫号记录添加
						if(gxsqBaseMapper.intoQueue(queueStart) >= 1){
							if(gxsqBaseMapper.intoHQueue(queueStart) >= 1){
								queueStart.setName(queueStart.getName().substring(0,1));
								queueStart.setIdcard(queueStart.getIdcard().substring(0,5)+"********"+queueStart.getIdcard().substring(13,idcard.length()));
								//取号信息推送
								send.sendSMSQueueInfo(queueStart.getTell(),queueStart.getNumber(),queueStart.getBusinessname(),this.qhQueueValue(queueStart.getTypeid(),queueStart.getNumber()));
								return AjaxJson.success().put("data",queueStart);
							}
						}
						return AjaxJson.error("添加排队记录失败！");
					}
				}
			}else{
				return AjaxJson.error("有未办结记录！");
			}
		}else{
			return AjaxJson.error("非工作日时间！");
		}
	}


	/**
	 * jhCardQueue
	 * 排队叫号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@Transactional(readOnly = false)
	public AjaxJson jhCardQueue(String countid,String winno,String areaid,String max){
		//叫号上锁
		synchronized (this){
			//判断当前窗口是否有未办理完的件
			String status = this.checkQueue(countid);
			if(null != status  && status.equals("1")){
				return AjaxJson.error("当前有未办结件！");
			}
			//获取当前排队信息
			HashMap <String, String> queue = gxsqBaseMapper.startQueue(countid, areaid);
			if(null != queue){
				//删除排队信息
				if(gxsqBaseMapper.delQueue(queue.get("queueid")) >= 1){
					//推送叫号信息
					if(!"true".equals(queue.get("isrun"))){
						send.sendSMSInfo(queue.get("number"),queue.get("tell"),winno);
					}
					//修改排队记录
					if(gxsqBaseMapper.updateHqueue(queue.get("queueid"),DateUtils.getDateTime(),countid) >= 1 && gxsqBaseMapper.updateCountNumber(queue.get("number"),countid) >= 1){
						queue.put("status","1");
						queue.put("num",gxsqBaseMapper.waitValue(countid,areaid));
						//推送评价仪消息
						Map<String,String> map = new HashMap <>(2);
						map.put("type","sh");
						map.put("number",queue.get("number"));
						webSocketServer.sendMessage(JSONObject.fromObject(map).toString(), WebSocketServer.tokenAndSessionMap.get(max));
						return AjaxJson.success().put("data",queue);
					}
				}
			}
			return AjaxJson.error("暂无该排队信息！");
		}
	}

	/**
	 * serviceCardQueue
	 * 开始服务
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public String checkQueue(String countid){
		//判断当前窗口是否有未办理完的件
		return gxsqBaseMapper.checkQueue(countid);
	}

	/**
	 * chCardQueue
	 * 重呼
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public AjaxJson chCardQueue(String id,String winno,String number){
		//推送叫号信息
		HashMap <String, String> map = gxsqBaseMapper.getQueueTell(id);
		if(!"true".equals(map.get("isrun"))){
			send.sendSMSInfo(number,map.get("tell"),winno);
		}
		return AjaxJson.success();
	}

	/**
	 * serviceCardQueue
	 * 开始服务
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@Transactional(readOnly = false)
	public AjaxJson serviceCardQueue(String id){
		//修改排队记录
		String times = DateUtils.getDateTime();
		gxsqBaseMapper.updateServiceTime(id,times);
		return AjaxJson.success().put("times",times);
	}

	/**
	 * endCardQueue
	 * 结束服务
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@Transactional(readOnly = false)
	public AjaxJson endCardQueue(String id,String max){
		//推送评价仪消息
		if(StringUtils.isNotBlank(max)){
			Map<String,String> map = new HashMap <>(1);
			map.put("type","bj");
			webSocketServer.sendMessage(JSONObject.fromObject(map).toString(), WebSocketServer.tokenAndSessionMap.get(max));
		}
		//修改排队记录
		gxsqBaseMapper.updateEndTime(id,DateUtils.getDateTime());
		return AjaxJson.success().put("status","2");
	}

	/**
	 * cancleCardQueue
	 * 弃号
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	@Transactional(readOnly = false)
	public AjaxJson cancleCardQueue(String id,String max){
		//推送评价仪消息
		if(StringUtils.isNotBlank(max)){
			Map<String,String> map = new HashMap <>(2);
			map.put("type","qh");
			webSocketServer.sendMessage(JSONObject.fromObject(map).toString(), WebSocketServer.tokenAndSessionMap.get(max));
		}
		//修改排队记录
		gxsqBaseMapper.cancleCardQueue(id);
		return AjaxJson.success().put("status","-1");
	}

	/**
	 * findAreaList
	 * 获取点位列表
	 * @author azj
	 * @date 2020-05-18/下午1:43:15
	 */
	public List <HashMap <String,Object>>findAreaList(){
		List <HashMap<String,Object>> areaList = gxsqBaseMapper.findAreaList();
		List <HashMap<String,Object>> data = new ArrayList <>();
		//获取区级点位
		for(HashMap<String,Object> m : areaList){
			if(null == m.get("pid")){
				m.put("children",this.toChildLise(areaList,m.get("areacode").toString()));
				data.add(m);
			}
		}
		return data;
	}

	public List <HashMap<String,Object>> toChildLise(List <HashMap<String,Object>> areaList,String areacode){
		//获取区级点位
		List <HashMap<String,Object>> data = new ArrayList <>();
		for(HashMap<String,Object> m : areaList){
			if(null != m.get("pid") && m.get("pid").equals(areacode)){
				m.put("children",this.toChildLise(areaList,m.get("areacode").toString()));
				data.add(m);
			}
		}
		return data;
	}

	@Transactional(readOnly = false)
	public CountQueue queueList(String userid){
		String areaid = gxsqBaseMapper.officeValue(userid);
		//加载窗口数据及排队信息
		CountQueue queue = gxsqBaseMapper.queueList(userid, areaid);
		queue.setAreaid(areaid);
		queue.setCount(gxsqBaseMapper.waitValue(queue.getCountid(),areaid));
		return queue;
	}
	@Transactional(readOnly = false)
	public Integer updateCountStatus(String status,String userid){
		//修改窗口状态
		return gxsqBaseMapper.updateCountStatus(status,userid);
	}

	public AjaxJson findCountStatus(String max){
		//获取窗口状态
		return AjaxJson.success().put("data",gxsqBaseMapper.findCountStatus(max));
	}

	public AjaxJson findCountValue(String countid){
		//获取窗口办理信息
		return AjaxJson.success().put("data",gxsqBaseMapper.findCountValue(countid));
	}

	@Transactional(readOnly = false)
	public Integer updatePJ(String pjlevel,String id){
		return gxsqBaseMapper.updatePJ(pjlevel,id);
	}

	public List <String> qhQueueValue(String typeid, String number){
		return gxsqBaseMapper.qhQueueValue(typeid,number);
	}

	public List <HashMap<String,String>> getTypeName(String countid){
		return gxsqBaseMapper.getTypeName(countid);
	}

	@Transactional(readOnly = false)
	public void delDayQueue(){
		gxsqBaseMapper.delDayQueue();
		gxsqBaseMapper.updateAllCountNumber();
	}
}